package com.yisu.kabao.manager.domain.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
 * Created by xlt on 2017/3/29.
 */
public class UnCompressFile {
    private static final int BUFFEREDSIZE = 1024;
    private static UnCompressFile instance = new UnCompressFile();

    private UnCompressFile() {
    }

    public static UnCompressFile getInstance() {
        return instance;
    }



    // //////////////////////////解压缩////////////////////////////////////////
    /**
     * 调用org.apache.tools.zip实现解压缩，支持目录嵌套和中文名
     * 也可以使用java.util.zip不过如果是中文的话，解压缩的时候文件名字会是乱码。原因是解压缩软件的编码格式跟java.util.zip.ZipInputStream的编码字符集(固定是UTF-8)不同
     *
     * @param zipFilePath
     *            要解压缩的文件
     * @param targetPath
     *            要解压到的目录
     * @throws Exception
     */
    public static boolean unzip(String zipFilePath, String targetPath) throws IOException {
        InputStream is = null;
        FileOutputStream fileOut = null;
        File file = null;
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(zipFilePath,"GBK");
            String directoryPath = "";
            if (null == targetPath || "".equals(targetPath)) {
                directoryPath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));
            } else {
                directoryPath = targetPath;
            }
            for(Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();){
                ZipEntry entry = (ZipEntry)entries.nextElement();
                file = new File(directoryPath+"/"+entry.getName());
                if(entry.isDirectory()){
                    file.mkdirs();
                }else{
                    //如果指定文件的目录不存在,则创建之.
                    File parent = file.getParentFile();
                    if(!parent.exists()){
                        parent.mkdirs();
                    }
                    is = zipFile.getInputStream(entry);
                    fileOut = new FileOutputStream(file);
                    int readLen = 0;
                    byte[] buffer = new byte[4096];
                    while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
                        fileOut.write(buffer, 0, readLen);
                    }
                    fileOut.close();
                    is.close();
                }
            }
            zipFile.close();
        } catch (Exception ex) {
            throw ex;
        } finally {
            if(null != zipFile){
                zipFile = null;
            }
            if (null != is) {
                is.close();
            }
            if (null != fileOut) {
                fileOut.close();
            }
        }
        return true;
    }

    /**
     * 创建目录
     *
     * @param directory
     *            父目录
     * @param subDirectory
     *            子目录
     */
    private static void createDirectory(String directory, String subDirectory) {
        String dir[];
        File fl = new File(directory);
        try {
            if (subDirectory == "" && fl.exists() != true)
                fl.mkdir();
            else if (subDirectory != "") {
                dir = subDirectory.replace('\\', '/').split("/");
                for (int i = 0; i < dir.length; i++) {
                    File subFile = new File(directory + File.separator + dir[i]);
                    if (subFile.exists() == false)
                        subFile.mkdir();
                    directory += File.separator + dir[i];
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }



}
